package com.bsh.repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.PartnerUpgradeAskMapper;
import com.bsh.mybatis.model.PartnerUpgradeAsk;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 合伙人升级申请
 * 
 * @author gsh
 *
 */
@Repository
public class DaoPartnerUpgradeAsk {
	@Resource
	SqlSessionTemplate session;

	PartnerUpgradeAskMapper getPartnerUpgradeAskMapper() {
		return session.getMapper(PartnerUpgradeAskMapper.class);
	}

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;

	PartnerUpgradeAskMapper getMapper() {
		return session.getMapper(PartnerUpgradeAskMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(PartnerUpgradeAsk record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(PartnerUpgradeAsk record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public PartnerUpgradeAsk selectByPrimaryKey(Integer id) {
		PartnerUpgradeAsk row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(PartnerUpgradeAsk record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(PartnerUpgradeAsk record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	/**
	 * 成为合作伙伴分页查询
	 */
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select a.*,e.name up_level_name,ev.name level_name, ");
		sb.append("case  ");
		sb.append("  when a.audit_status=0 then '待审核' ");
		sb.append("  when a.audit_status=1 then '已通过' ");
		sb.append("  when a.audit_status=2 then '未通过' ");
		sb.append("end audit_status_ch, ");
		sb.append("p.country,p.province,p.city,p.area,p.real_name,p.userid, ");
		sb.append("u.head_img,u.name user_name,u.mobile, ");
		sb.append("rc.name country_name,rp.name province_name,rt.name city_name,ra.name area_name ");
		sb.append("from partner_upgrade_ask a ");
		sb.append("left join partner_level e on e.id=a.up_level ");
		sb.append("left join partner_level ev on ev.id=a.level ");
		sb.append("left join partner p on p.id=a.partner_id ");
		sb.append("left join region rc on rc.id=p.country ");
		sb.append("left join region rp on rp.id=p.province ");
		sb.append("left join region rt on rt.id=p.city ");
		sb.append("left join region ra on ra.id=p.area ");
		sb.append("left join user u on u.id=p.userid ");

		sb.append("where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		StringBuilder sb2 = new StringBuilder();
		sb2.append("select * from (" + sb.toString() + ")v order by v.id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb2.toString(), ps);

		pager.setRowCount(count);
		return list;
	}
	
	/**
	 * 审核
	 * @param userid
	 * @return
	 */
	public Integer audit(int askId,int partnerId, boolean isPass, String auditMsg) {
		PartnerUpgradeAskMapper map = this.getMapper();
		if (isPass)
			return map.auditPass(askId,partnerId, new Date());
		else
			return map.auditRefuse(askId,partnerId, auditMsg, new Date());
	}
	
	/**
	 * 查询申请合伙人升级记录
	 * @param userId
	 * @return
	 */
	public List<Map<String, Object>> getUpgradePartnerAskByPartnerId(Integer partnerId,int status) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" SELECT m.*");
		sb.append(" FROM partner_upgrade_ask m ");
		sb.append(" where 1=1 ");
		sb.append(String.format(" AND  m.audit_status = %d ", status ));
		sb.append(String.format(" AND  m.partner_id = %d ", partnerId ));
		sb.append(" order by m.create_time desc  ");
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);
		return list;
	}
}
